﻿using Common;
using DAL.DBUtility;
using Model;
using Model.AntiFake;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Enums;
using Enums.Sys;
namespace DAL.AntiFake
{
   public class QueryCodeLogDAL
    {
       /// <summary>
       /// 表是否存在
       /// </summary>
       /// <param name="fw_code"></param>
       /// <returns></returns>
//       public TableModel TabIsExist(string fw_code)
//       {
//           //防伪码字符处理获取表后缀信息
//               string real_fw_code = MiMa.Jiemi(fw_code, AppConst.DigitCodeKey);
//               string tabSuffix = AppConst.TablePrefix + real_fw_code.Substring(0, 6);//获取到表名
//               string sql = @"select name from dbo.sysobjects where id=object_id(N'[dbo].[@TableName]') 
//                and OBJECTPROPERTY(id,N'IsUserTable')=1";
//               TableModel model = new TableModel();
//               SqlParameter[] parmars ={
//                    new SqlParameter("@TableName",tabSuffix)
//               };
//               SqlDataReader sdr = DbHelperSQL.ExecuteReader(sql, parmars);
//               if (sdr.Read())
//               {
//                   model = CommonModel(sdr);
//               }
//               sdr.Close();
//               return model;
          
           
          
//       }

       private TableModel CommonModel(SqlDataReader sdr)
       {
           TableModel model = new TableModel();

           model.name = PageValidate.ConvertToTrimString(sdr["name"]);
           return model;
       }

       /// <summary>
       /// 根据防伪码获取对应的防伪信息
       /// </summary>
       /// <returns></returns>
       public AntiFake_DigitcodeModel GetModelByFw_Code(string fw_code)
       {
       
           AntiFake_DigitcodeModel model = new AntiFake_DigitcodeModel();
           string real_fw_code = MiMa.Jiemi(fw_code, AppConst.DigitCodeKey);
           string tabSuffix = AppConst.TablePrefix + real_fw_code.Substring(0, 6);//获取到表名
           string sql = @"  SELECT *,oe.EnterpriseName FROM " + tabSuffix + " aa "+
"left join Organization_Enterprise oe on aa.EnterpriseId=oe.ID WHERE aa.Fw_CodeSecret=@Fw_CodeSecret AND aa.ActiveType='0' AND aa.DisableTime >=GETDATE() ";
           SqlParameter[] parmars ={
                   
                    new SqlParameter("@Fw_CodeSecret",fw_code)
               };
           SqlDataReader sdr = DbHelperSQL.ExecuteReader(sql, parmars);
           if (sdr.Read())
           {
               model = CommonModelForDigitcode(sdr);
           }
           sdr.Close();
           return model;

       }

       private AntiFake_DigitcodeModel CommonModelForDigitcode(SqlDataReader sdr)
       {
           AntiFake_DigitcodeModel model = new AntiFake_DigitcodeModel();
           model.ID = PageValidate.ConvertToTrimInt(sdr["ID"]);
           model.Fw_Code = PageValidate.ConvertToTrimString(sdr["Fw_Code"]);
           model.Fw_CodeSecret = PageValidate.ConvertToTrimString(sdr["Fw_CodeSecret"]);
           model.Wl_Code = PageValidate.ConvertToTrimString(sdr["Wl_Code"]);
           model.WB_Fw_Code = PageValidate.ConvertToTrimString(sdr["WB_Fw_Code"]);
           model.WB_Wl_Code = PageValidate.ConvertToTrimString(sdr["WB_Wl_Code"]);
           model.EnterpriseId = PageValidate.ConvertToTrimInt(sdr["EnterpriseId"]);
           model.ZEnterpriseId = PageValidate.ConvertToTrimInt(sdr["ZEnterpriseId"]);
           model.Stats = PageValidate.ConvertToTrimInt(sdr["Stats"]);
           model.QueryCount = PageValidate.ConvertToTrimInt(sdr["QueryCount"]);
           model.QueryTime = PageValidate.ConvertToDateTime(sdr["QueryTime"]);
           model.CreateDate = PageValidate.ConvertToDateTime(sdr["CreateDate"]);
           model.ChangeDate = PageValidate.ConvertToDateTime(sdr["ChangeDate"]);
           model.ActiveType = PageValidate.ConvertToTrimInt(sdr["ActiveType"]);
           model.DisableTime = PageValidate.ConvertToDateTime(sdr["DisableTime"]);
           model.BS = PageValidate.ConvertToTrimInt(sdr["BS"]);
           model.TempletId = PageValidate.ConvertToTrimInt(sdr["TempletId"]);
           model.CodeType = PageValidate.ConvertToTrimString(sdr["CodeType"]);
           model.EnterpriseName = PageValidate.ConvertToTrimString(sdr["EnterpriseName"]);
           return model;
       }

       public bool TabIsExist(string fw_code, out string TableName)
       {
           DataTable tbl = new DataTable();
           bool flag = false;
           try {
               //防伪码字符处理获取表后缀信息
               string real_fw_code = MiMa.Jiemi(fw_code, AppConst.DigitCodeKey);
               string tabSuffix = AppConst.TablePrefix + real_fw_code.Substring(0, 6);//获取到表名
               string sql = @"select * from {0}";
               sql = string.Format(sql, tabSuffix);
               tbl = DbHelperSQL.GetTable(sql);
               flag = true;
               TableName=tabSuffix;
           }
           catch(Exception ex){
               TableName = "";
           }
         
           return flag;

       }
       /// <summary>
       /// 修改查询次数
       /// </summary>
       /// <param name="model"></param>
       /// <param name="tableName"></param>
       /// <returns></returns>
       public ResultEntity UpdateDigitcode(AntiFake_DigitcodeModel model,string tableName) {
           string sql = @"  update " + tableName + " set QueryCount=@QueryCount,QueryTime=@QueryTime where ID=@ID";
           SqlParameter[] parameters = { new SqlParameter("@QueryCount",model.QueryCount),
                                        new SqlParameter("@QueryTime",model.QueryTime), 
                                        new SqlParameter("@ID",model.ID),
                                      };
           return DbHelperSQL.CommonResult(DbHelperSQL.ExecuteSql(sql, parameters));
       }
       /// <summary>
       /// 添加日志
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
       public ResultEntity AddQueryCodeLog(QueryCodeLogModel model) {
           string sql = @"  insert into AntiFake_QueryCodeLog(Fw_Code,QueryType,QueryValue,QueryTime,QueryMessage,Valid,QueryProvince,QueryCity,ProductId,EnterpriseId) values
  (@Fw_Code,@QueryType,@QueryValue,@QueryTime,@QueryMessage,@Valid,@QueryProvince,@QueryCity,@ProductId,@EnterpriseId)";
           SqlParameter[] parameters = {
                                      new SqlParameter("@Fw_Code",model.Fw_Code),
                                        new SqlParameter("@QueryType",model.QueryType),
                                          new SqlParameter("@QueryValue",model.QueryValue),
                                            new SqlParameter("@QueryTime",model.QueryTime),
                                              new SqlParameter("@QueryMessage",model.QueryMessage),
                                                new SqlParameter("@Valid",model.Valid),
                                                  new SqlParameter("@QueryProvince",model.QueryProvince),
                                                  new SqlParameter("@QueryCity",model.QueryCity),
                                                   new SqlParameter("@ProductId",model.ProductId),
                                                  new SqlParameter("@EnterpriseId",model.EnterpriseId),
                                      };
           return DbHelperSQL.CommonResult(DbHelperSQL.ExecuteSql(sql, parameters));

       }
       public List<QueryCodeLogModel> GetListByCode(string code) {
           string sql = @"  select * from AntiFake_QueryCodeLog where Fw_Code=@code";
           SqlParameter[] parameters = { new SqlParameter("@code", code) };
           DataTable dt = DbHelperSQL.Query(sql, parameters).Tables[0];
           List<QueryCodeLogModel> modelList = ListDataSet.DataSetToIList<QueryCodeLogModel>(dt).ToList();
           return modelList;
       }

       /// <summary>
       /// 查询分页列表
       /// </summary>
       /// <param name="strWhere"></param>
       /// <returns></returns>
       public List<QueryCodeLogModel> GetList(string strWhere)
       {
           //  string sql = @"  select * from Sys_User where ViewDel=@ViewDel and EnterpriseId=@EnterpriseId";
           string sql = @"select aq.ID,aq.Fw_Code,case  aq.QueryType when 0 then '网站' when 4 then '微信' else '' end as QueryType,
                 aq.QueryValue,aq.QueryTime,aq.QueryProvince,aq.QueryCity,oe.EnterpriseName
                 
                 from AntiFake_QueryCodeLog aq left join Organization_Enterprise oe
                 on aq.EnterpriseId=oe.ID {0}";
           sql = string.Format(sql, strWhere);
           SqlParameter[] parameters = {
                                            new SqlParameter("@ViewDel", EnumManagement.GetEnumValue(SysEnum.NoDele))
			};
           return ListDataSet.DataSetToIList<QueryCodeLogModel>(DbHelperSQL.Query(sql, parameters)).ToList();
       }
    }
}
